require( '$d/css/common.less' );
require( './style.less' );

define( [
  'jquery',
  'require',
  'header',
  'message',
  'nav',
  'api',
  'pagination',
  'bigwe_component'
], function( $, require ) {
  var bu = require( 'bigwe_utils' );
  var ShowTitleMessage = require( 'message' ),
    showTime = ShowTitleMessage.showTime,
    BIGWE_API = require( 'api' ),
    thisApis = BIGWE_API.NOTICE,
    bc = require( 'bigwe_component' )
  $( '#inquiry' ).click( function() {
    load();
  } );
  function load() {
    if ( parseInt( document.getElementById( 'zhichiBtnBox' ).style.bottom ) == 0 ) {
      document.getElementById( "zhichiBtn" ).click();
    } else {
      return;
    }
  }

  var check_one = $( '.notice_data_bt .fl input' ),
    check_all = $( '.notice_menu_lf input.checkall' );
  $( '.notice_nr' ).on( 'click', '.notice_data_bt .fl .tittle', function() {
    if ( $( this ).parents( 'li' ).hasClass( 'active' ) ) {
      $( this ).parents( 'li' ).removeClass( 'active' );
      $( this ).parents( '.notice_data_bt' ).next( '.notice_data_nr' ).slideUp();
    } else {
      $( this ).parents( '.notice_data_bt' ).next( '.notice_data_nr' ).slideDown();
      $( '.notice_data ul li' ).removeClass( 'active' );
      $( this ).parents( 'li' ).prop( 'class', 'readed0 active' );
      $( '.notice_data ul li' ).not( '.active' ).find( '.notice_data_nr' ).slideUp();
    }
    var target_id = $( this ).parents( 'li' ).attr( "smid" ),
      object = "";
    object = target_id + ",";
    readed( object );

  } );
  //标记已读
  $( '.notice_menu_rgt a.read' ).click( function() {
    var object = "";

    $( '.notice_data ul li' ).each( function() {
      if ( $( this ).find( 'input[type=checkbox]' ).get( 0 ).checked ) {
        $( this ).prop( 'class', 'readed0' );
        object += $( this ).attr( "smid" );
        object += ",";
      }
    } );
    if ( object.length == 0 ) {
      ShowTitleMessage.showWarning( '请选择要标记的内容！', showTime );
      return;
    }

    readed( object );
  } );

  //重新发送
  $( document ).on( 'click', '.hanssagain', function() {
    var ss = $( this ).parents( '.notice_data_nr' ).find( 'span:first' );

    var mid = ss.attr( 'hans_id' );
    var articleTitle = ss.attr( 'hans_title' );
    var app_id = ss.attr( 'hans_app_id' );
    $.ajax( {
      type: 'post',
      url: thisApis.SEND_AGAIN,
      timeout: 1000,
      data: {
        id: mid,
        app_id: app_id,
        title: articleTitle,
      },
      complete: function() {
        ShowTitleMessage.show( '正在重发…', showTime );
        //ssta.parents('.detail').find('.txt-msgstatus').html('发送中');
      }
    } );
  } );
  // 全选
  check_all.click( function() {
    if ( $( this ).get( 0 ).checked ) {
      $( '.notice_data input[type=checkbox]' ).each( function( i ) {
        $( '.notice_data input[type=checkbox]' ).eq( i ).get( 0 ).checked = true;
      } );
    } else {
      $( '.notice_data input[type=checkbox]' ).each( function( i ) {
        $( '.notice_data input[type=checkbox]' ).eq( i ).get( 0 ).checked = false;
      } );
    }
  } );
  // 删除全选
  check_one.click( function() {
    check_all.get( 0 ).checked = false;
  } );

  //已读未读
  $( ".dropdown-menu>li>a" ).click( function() {
    var _text = $( this ).text(),
      _val = $( this ).attr( 'read-val' );
    $( this ).parents( '.dropdown' ).find( '.filter_option' ).text( _text );
    record.options.status = _val;
    record.options.p = 1;
    isOk = true;
    record.init();
  } );

  //升序降序
  $( ".notice_menu_lf .rank i" ).click( function() {
    var _val = $( this ).attr( 'time' );
    $( this ).addClass( 'active' ).siblings().removeClass( 'active' );
    record.options.time = _val;
    isOk = true;
    record.init();
  } );

  var isOk = true;
  var current_page = 0;
  var record = {
    options: {
      p: 1,
      time: 'desc',
      l: 20,
    },
    init: function() {
      ShowTitleMessage.show( '正在加载...' );
      this.list( this.options );
      if ( isOk == true ) {
        $( '.paging' ).hide();
      }
    },
    list: function( event ) {
      var _this = this;
      $.ajax( {
        type: "post",
        dataType: "json",
        url: thisApis.GET_LIST,
        data: event,
        success: function( data ) {
          if ( data.data.count == 0 ) {
            $( '.notice_nr' ).empty().append( "<p>暂无数据!</p>" );
            ShowTitleMessage.hide();
          } else {
            _this.list_content( data );
            if ( isOk == true && data.data.count > 10 ) {
              _this.paging( data.data.count );
            }
            if ( localStorage.notice_num != '0' ) {
              localStorage.notice_num = data.data.unread_count;
              if ( localStorage.notice_num == '0' ) {
                $( '.user_notice .num' ).hide();
                $( '.user_notice .icon-tongzhi' ).show();
              } else {
                // 2017年7月28日 Sherlock 不用隐藏图标
                //$( '.user_notice .icon-tongzhi' ).hide();
                $( '.user_notice .num' ).css( 'display', 'inline-block' );
                $( '.top_main_nav .user_notice .num' ).text( data.data.unread_count );
              }
            }
          }
        }
      } )
    },
    list_content: function( argument ) {
      var artlist_html = '',
        arts = argument.data.results;
      var shijian = new Date( arts[ 0 ].create_time * 1000 ).format( 'MM-dd' ),
        notice_data_len = 1,
        shuju = new Array(),
        shuju_nr = new Array();

      $( '.notice_wrap .tip span.red' ).text( argument.data.unread_count );
      for ( i in arts ) {
        var shijian2 = new Date( arts[ i ].create_time * 1000 ).format( 'MM-dd' );

        if ( shijian === shijian2 ) {
          shuju_nr.push( arts[ i ] );
        } else {
          shuju.push( shuju_nr );
          shijian = new Date( arts[ i ].create_time * 1000 ).format( 'MM-dd' );
          notice_data_len++;
          shuju_nr = [];
          shuju_nr.push( arts[ i ] );
        }
      }
      shuju.push( shuju_nr );

      for ( var i in shuju ) {

        var mouth = new Date();
        var date = new Date();
        var arts_html = '';
        for ( var j in shuju[ i ] ) {
          var art = shuju[ i ][ j ];
          var time = new Date( art.create_time * 1000 ).format( 'hh:mm' );

          mouth = new Date( art.create_time * 1000 ).getMonth() + 1;
          date = new Date( art.create_time * 1000 ).getDate();
          arts_html += '<li class="readed' + art.status + '" smid="' + art.smid + '">' +
            '<div class="notice_data_bt clearfix">' +
            '<div class="fl clearfix">' +
            '<input type="checkbox" class="fl">' +
            '<span class="fl">' + time + '</span>' +
            '<div class="tittle fl">' + art.title + '</div>' +
            '</div>' +
            '<span class="fr glyphicon glyphicon-trash"> </span>' +
            '</div>' +
            '<div class="notice_data_nr">' + art.content + '</div>' +
            '</li>'
        }
        artlist_html += '<div class="notice_data">' +
          '<div class="date">' + mouth + '月' + date + '日' + '</div>' +
          '<ul>' + arts_html +
          '</ul>' +
          '</div>'

      }
      $( '.notice_nr' ).empty().append( artlist_html );

      ShowTitleMessage.hide();
    },
    paging: function( argument ) {
      $( ".paging" ).pagination( argument, {
        num_edge_entries: 1,
        num_display_entries: 10,
        items_per_page: 20,
        prev_text: '上一页',
        next_text: '下一页',
        current_page: current_page,
        callback: function( _index, jq ) {
          if ( isOk == true ) {
            isOk = false;
            return;
          } else {
            record.options.p = _index + 1;
            record.init();
          }
        }
      } );
      $( '.paging' ).show();
    }
  }

  record.init();


  //删除包裹元素
  function delete_wrap() {
    $( '.notice_data' ).each( function() {
      var len = $( this ).find( 'ul li' ).length;
      if ( len == 0 ) {
        $( this ).remove();
      }
    } );
  }
  /*单个删除*/
  $( ".notice_nr" ).on( "click", "span.glyphicon-trash", function() {
    var target_id = $( this ).parents( 'li' ).attr( "smid" ),
      _this = $( this ),
      num = $( '.notice_wrap .tip span.red' ).text(),
      object = "";
    bc.deleteConfirmShowBottom( {
      obj: this,
      fun: function() {
        _this.parents( 'li' ).remove();
        if ( _this.parents( 'li' ).hasClass( 'readed1' ) ) {
          $( ".notice_wrap .tip span.red" ).text( num - 1 );
        }
        object = target_id + ",";

        deletedata( object );
      }
    } );
  } );

  /*批量删除*/
  var _object = [];
  $( ".notice_menu_rgt a.delete" ).click( function() {
    var len = $( '.notice_nr' ).find( 'input:checked' ).length;
    if ( len == 0 ) {
      ShowTitleMessage.showWarning( '请选择要删除的内容！', showTime );
      return;
    }
    var object = "",
      num = $( '.notice_wrap .tip span.red' ).text(),
      l = 0;
    bc.deleteConfirmShowBottom( {
      obj: this,
      fun: function() {
        $( '.notice_data ul li' ).each( function() {
          if ( $( this ).find( 'input[type=checkbox]' ).get( 0 ).checked ) {
            object += $( this ).attr( "smid" );
            object += ",";
            _object.push( $( this ).attr( "smid" ) );
            if ( $( this ).hasClass( 'readed1' ) ) {
              l++;
            }
            $( this ).remove();
          }
        } );
        $( ".notice_wrap .tip span.red" ).text( num - l );
        menu_rgt = true;
        deletedata( object );
      }
    } );
  } );


  /*删除通知*/
  function deletedata( targetid ) {

    $( 'input.checkall.fl' ).attr( 'checked', false );
    var page_total = $( '.next' ).prev().text();
    var current = $( '.current' ).text().replace( /[^0-9]/ig, "" );
    var num = $( '.tip > .red' ).text() % 20;
    if ( num == 0 || _object.length == 20 ) {
      _object = [];
      if ( current != 1 ) {
        if ( current == page_total ) {
          current_page = current - 2;
          record.options.p = current - 1;
        } else {
          current_page = current - 1;
        }
      }
      isOk = true;
    }
    $.ajax( {
      type: 'post',
      dataType: 'json',
      url: thisApis.DEL_MESSAGE,
      data: {
        smid: targetid
      },
      success: function( data ) {
        if ( data.ret == 0 ) {
          record.init();
        }
      }
    } );
    delete_wrap();
  }
  /*标记已读*/
  function readed( targetid ) {
    $.ajax( {
      type: 'post',
      dataType: 'json',
      url: thisApis.READ_MESSAGE,
      data: {
        smid: targetid
      },
      success: function( data ) {
        if ( localStorage.notice_num != '0' ) {
          localStorage.notice_num = localStorage.notice_num - data.data;
          $( '.notice_wrap .tip span.red' ).text( localStorage.notice_num );
          $( '.top_main_nav .user_notice .num' ).text( localStorage.notice_num );
          if ( localStorage.notice_num == '0' ) {
            $( '.user_notice .num' ).hide();
            $( '.user_notice .icon-tongzhi' ).show();
          }
        }
      }
    } )
  }

} )